home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / email / quoprimime.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  286 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Quoted-printable content transfer encoding per RFCs 2045-2047.
  5.  
  6. This module handles the content transfer encoding method defined in RFC 2045
  7. to encode US ASCII-like 8-bit data called `quoted-printable'.  It is used to
  8. safely encode text that is in a character set similar to the 7-bit US ASCII
  9. character set, but that includes some 8-bit characters that are normally not
  10. allowed in email bodies or headers.
  11.  
  12. Quoted-printable is very space-inefficient for encoding binary files; use the
  13. email.base64mime module for that instead.
  14.  
  15. This module provides an interface to encode and decode both headers and bodies
  16. with quoted-printable encoding.
  17.  
  18. RFC 2045 defines a method for including character set information in an
  19. `encoded-word' in a header.  This method is commonly used for 8-bit real names
  20. in To:/From:/Cc: etc. fields, as well as Subject: lines.
  21.  
  22. This module does not do the line wrapping or end-of-line character
  23. conversion necessary for proper internationalized headers; it only
  24. does dumb encoding and decoding.  To deal with the various line
  25. wrapping issues, use the email.header module.
  26. """
  27. __all__ = [
  28.     'body_decode',
  29.     'body_encode',
  30.     'body_quopri_check',
  31.     'body_quopri_len',
  32.     'decode',
  33.     'decodestring',
  34.     'encode',
  35.     'encodestring',
  36.     'header_decode',
  37.     'header_encode',
  38.     'header_quopri_check',
  39.     'header_quopri_len',
  40.     'quote',
  41.     'unquote']
  42. import re
  43. from string import hexdigits
  44. from email.utils import fix_eols
  45. CRLF = '\r\n'
  46. NL = '\n'
  47. MISC_LEN = 7
  48. hqre = re.compile('[^-a-zA-Z0-9!*+/ ]')
  49. bqre = re.compile('[^ !-<>-~\\t]')
  50.  
  51. def header_quopri_check(c):
  52.     '''Return True if the character should be escaped with header quopri.'''
  53.     return bool(hqre.match(c))
  54.  
  55.  
  56. def body_quopri_check(c):
  57.     '''Return True if the character should be escaped with body quopri.'''
  58.     return bool(bqre.match(c))
  59.  
  60.  
  61. def header_quopri_len(s):
  62.     '''Return the length of str when it is encoded with header quopri.'''
  63.     count = 0
  64.     for c in s:
  65.         if hqre.match(c):
  66.             count += 3
  67.             continue
  68.         count += 1
  69.     
  70.     return count
  71.  
  72.  
  73. def body_quopri_len(str):
  74.     '''Return the length of str when it is encoded with body quopri.'''
  75.     count = 0
  76.     for c in str:
  77.         if bqre.match(c):
  78.             count += 3
  79.             continue
  80.         count += 1
  81.     
  82.     return count
  83.  
  84.  
  85. def _max_append(L, s, maxlen, extra = ''):
  86.     if not L:
  87.         L.append(s.lstrip())
  88.     elif len(L[-1]) + len(s) <= maxlen:
  89.         L[-1] += extra + s
  90.     else:
  91.         L.append(s.lstrip())
  92.  
  93.  
  94. def unquote(s):
  95.     '''Turn a string in the form =AB to the ASCII character with value 0xab'''
  96.     return chr(int(s[1:3], 16))
  97.  
  98.  
  99. def quote(c):
  100.     return '=%02X' % ord(c)
  101.  
  102.  
  103. def header_encode(header, charset = 'iso-8859-1', keep_eols = False, maxlinelen = 76, eol = NL):
  104.     '''Encode a single header line with quoted-printable (like) encoding.
  105.  
  106.     Defined in RFC 2045, this `Q\' encoding is similar to quoted-printable, but
  107.     used specifically for email header fields to allow charsets with mostly 7
  108.     bit characters (and some 8 bit) to remain more or less readable in non-RFC
  109.     2045 aware mail clients.
  110.  
  111.     charset names the character set to use to encode the header.  It defaults
  112.     to iso-8859-1.
  113.  
  114.     The resulting string will be in the form:
  115.  
  116.     "=?charset?q?I_f=E2rt_in_your_g=E8n=E8ral_dire=E7tion?\\n
  117.       =?charset?q?Silly_=C8nglish_Kn=EEghts?="
  118.  
  119.     with each line wrapped safely at, at most, maxlinelen characters (defaults
  120.     to 76 characters).  If maxlinelen is None, the entire string is encoded in
  121.     one chunk with no splitting.
  122.  
  123.     End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted
  124.     to the canonical email line separator \\r\\n unless the keep_eols
  125.     parameter is True (the default is False).
  126.  
  127.     Each line of the header will be terminated in the value of eol, which
  128.     defaults to "\\n".  Set this to "\\r\\n" if you are using the result of
  129.     this function directly in email.
  130.     '''
  131.     if not header:
  132.         return header
  133.     if not keep_eols:
  134.         header = fix_eols(header)
  135.     
  136.     quoted = []
  137.     if maxlinelen is None:
  138.         max_encoded = 100000
  139.     else:
  140.         max_encoded = maxlinelen - len(charset) - MISC_LEN - 1
  141.     for c in header:
  142.         if c == ' ':
  143.             _max_append(quoted, '_', max_encoded)
  144.             continue
  145.         if not hqre.match(c):
  146.             _max_append(quoted, c, max_encoded)
  147.             continue
  148.         _max_append(quoted, '=%02X' % ord(c), max_encoded)
  149.     
  150.     joiner = eol + ' '
  151.     return []([ '=?%s?q?%s?=' % (charset, line) for line in quoted ])
  152.  
  153.  
  154. def encode(body, binary = False, maxlinelen = 76, eol = NL):
  155.     '''Encode with quoted-printable, wrapping at maxlinelen characters.
  156.  
  157.     If binary is False (the default), end-of-line characters will be converted
  158.     to the canonical email end-of-line sequence \\r\\n.  Otherwise they will
  159.     be left verbatim.
  160.  
  161.     Each line of encoded text will end with eol, which defaults to "\\n".  Set
  162.     this to "\\r\\n" if you will be using the result of this function directly
  163.     in an email.
  164.  
  165.     Each line will be wrapped at, at most, maxlinelen characters (defaults to
  166.     76 characters).  Long lines will have the `soft linefeed\' quoted-printable
  167.     character "=" appended to them, so the decoded text will be identical to
  168.     the original text.
  169.     '''
  170.     if not body:
  171.         return body
  172.     if not binary:
  173.         body = fix_eols(body)
  174.     
  175.     encoded_body = ''
  176.     lineno = -1
  177.     lines = body.splitlines(1)
  178.     for line in lines:
  179.         if line.endswith(CRLF):
  180.             line = line[:-2]
  181.         elif line[-1] in CRLF:
  182.             line = line[:-1]
  183.         
  184.         lineno += 1
  185.         encoded_line = ''
  186.         prev = None
  187.         linelen = len(line)
  188.         for j in range(linelen):
  189.             c = line[j]
  190.             prev = c
  191.             if bqre.match(c):
  192.                 c = quote(c)
  193.             elif j + 1 == linelen:
  194.                 if c not in ' \t':
  195.                     encoded_line += c
  196.                 
  197.                 prev = c
  198.                 continue
  199.             
  200.             if len(encoded_line) + len(c) >= maxlinelen:
  201.                 encoded_body += encoded_line + '=' + eol
  202.                 encoded_line = ''
  203.             
  204.             encoded_line += c
  205.         
  206.         if prev and prev in ' \t':
  207.             if lineno + 1 == len(lines):
  208.                 prev = quote(prev)
  209.                 if len(encoded_line) + len(prev) > maxlinelen:
  210.                     encoded_body += encoded_line + '=' + eol + prev
  211.                 else:
  212.                     encoded_body += encoded_line + prev
  213.             else:
  214.                 encoded_body += encoded_line + prev + '=' + eol
  215.             encoded_line = ''
  216.         
  217.         if lines[lineno].endswith(CRLF) or lines[lineno][-1] in CRLF:
  218.             encoded_body += encoded_line + eol
  219.         else:
  220.             encoded_body += encoded_line
  221.         encoded_line = ''
  222.     
  223.     return encoded_body
  224.  
  225. body_encode = encode
  226. encodestring = encode
  227.  
  228. def decode(encoded, eol = NL):
  229.     '''Decode a quoted-printable string.
  230.  
  231.     Lines are separated with eol, which defaults to \\n.
  232.     '''
  233.     if not encoded:
  234.         return encoded
  235.     decoded = ''
  236.     for line in encoded.splitlines():
  237.         line = line.rstrip()
  238.         if not line:
  239.             decoded += eol
  240.             continue
  241.         
  242.         i = 0
  243.         n = len(line)
  244.         while i < n:
  245.             c = line[i]
  246.             if c != '=':
  247.                 decoded += c
  248.                 i += 1
  249.             elif i + 1 == n:
  250.                 i += 1
  251.                 continue
  252.             elif i + 2 < n and line[i + 1] in hexdigits and line[i + 2] in hexdigits:
  253.                 decoded += unquote(line[i:i + 3])
  254.                 i += 3
  255.             else:
  256.                 decoded += c
  257.                 i += 1
  258.             if i == n:
  259.                 decoded += eol
  260.                 continue
  261.     
  262.     if not encoded.endswith(eol) and decoded.endswith(eol):
  263.         decoded = decoded[:-1]
  264.     
  265.     return decoded
  266.  
  267. body_decode = decode
  268. decodestring = decode
  269.  
  270. def _unquote_match(match):
  271.     '''Turn a match in the form =AB to the ASCII character with value 0xab'''
  272.     s = match.group(0)
  273.     return unquote(s)
  274.  
  275.  
  276. def header_decode(s):
  277.     """Decode a string encoded with RFC 2045 MIME header `Q' encoding.
  278.  
  279.     This function does not parse a full MIME header value encoded with
  280.     quoted-printable (like =?iso-8895-1?q?Hello_World?=) -- please use
  281.     the high level email.header class for that functionality.
  282.     """
  283.     s = s.replace('_', ' ')
  284.     return re.sub('=\\w{2}', _unquote_match, s)
  285.  
  286.